package net.ojbk.dao;

import java.sql.*;


import net.ojbk.database.DatabaseConnection;
import net.ojbk.po.WebConfig; // po层  数据实体类

/**
 * ActivationData Table
 * 
 * 增加 一条记录
 * 删除 一条记录
 * 查询 一条记录
 * 修改 一条记录
 * 
 * @author twobox
 *
 */
public class WebConfigDao {
	static final String tableName = "webConfig";
	DatabaseConnection dbconn; // 数据库连接控制对象
	Statement stmt; // sql语句 操作对象
	
	public WebConfigDao() {
		dbconn = new DatabaseConnection();
		stmt = dbconn.getStatement();
	}
	public void finalize() {
		dbconn.close();
	}
	
	// activationData(email, code, password)
	
	public boolean addOneRecord(WebConfig wc) {
		boolean flag = false;
		try {
			String sql = "INSERT INTO "
					+ tableName
					+ " VALUE('"
					+ wc.getX()
					+ "', '"
					+ wc.getY()
					+ "');";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteOneRecord(WebConfig wc) {
		boolean flag = false;
		try {
			// DELETE FROM activationData WHERE email='1@ojbk.net';
			String sql = "DELETE FROM "
					+ tableName
					+ " WHERE x='"
					+ wc.getX()
					+ "';";
			stmt.executeUpdate(sql);
			flag = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean updateOneRecord(WebConfig wc) {
		boolean flag = false;
		try {
			// UPDATE activationData SET code='124412412jk4kj124k', password='654321' WHERE email='1@ojbk.net';
			String sql = "UPDATE "
					+ tableName
					+ " SET x='"
					+ wc.getX()
					+ "', y='"
					+ wc.getY()
					+ "'where x='"+wc.getX()+"';";
			stmt.executeUpdate(sql);
			return true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean queryOneRecord(WebConfig wc) {
		boolean flag = false;
		ResultSet re;
		try {
			// SELECT * FROM activationData WHERE email='1@ojbk.net';
			String sql;
				sql = "SELECT * FROM "
						+ tableName
						+ " WHERE x='"
						+ wc.getX()
						+ "';";
			re = stmt.executeQuery(sql);
			if (re.next()) {
				wc.setX(re.getString("x"));
				wc.setY(re.getString("y"));
				flag = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public static void main(String[] args) {
		WebConfig wc=new WebConfig();
		WebConfigDao wcd=new WebConfigDao();
		wc.setX("shuxing");
		//wc.setY("zhi");
		//wcd.addOneRecord(wc);
		wc.setY("change");
		//wcd.updateOneRecord(wc);
		wcd.deleteOneRecord(wc);
	}
	
}
